﻿Imports System.Windows.Forms
Imports Microsoft.Feeds.Interop

Public Class Options
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If DwmIsCompositionEnabled() Then TreeView1 = New VistaControls.TreeView
        Dim images As New ImageList
        images.Images.Add(My.Resources.rssicon)
        images.Images.Add(My.Resources.folder)
        TreeView1.ImageList = images
        UpdateTree()
    End Sub

    Private Sub UpdateTree()
        TreeView1.Nodes.Clear()
        For Each Feed As IFeed In FeedsManager.RootFolder.Feeds
            TreeView1.Nodes.Add(Feed.Name, Feed.Name, 0)
        Next
        For Each Subfolder As IFeedFolder In FeedsManager.RootFolder.Subfolders
            TreeView1.Nodes.Add(GetFolders(Subfolder))
        Next
    End Sub

    Function GetFolders(ByVal Folder As IFeedFolder) As TreeNode
        Dim Tree As New TreeNode(Folder.Name, 1, 1)
        Tree.Name = Folder.Path
        For Each Feed As IFeed In Folder.Feeds
            Tree.Nodes.Add(Feed.Path, Feed.Name, 0)
        Next
        For Each Subfolder As IFeedFolder In Folder.Subfolders
            Tree.Nodes.Add(GetFolders(Subfolder))
        Next
        Return Tree
    End Function

    Private Sub RemoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveButton.Click
        Try : FeedsManager.DeleteFeed(TreeView1.SelectedNode.Name)
        Catch : FeedsManager.DeleteFolder(TreeView1.SelectedNode.Name)
        End Try
        UpdateTree()
    End Sub

    Private Sub AddFolderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFolderButton.Click
        Dim FolderName As String = InputBox("Please enter folder name")
        Dim folder As IFeedFolder
        Try : folder = FeedsManager.GetFolder(TreeView1.SelectedNode.Name)
        Catch : folder = FeedsManager.RootFolder
        End Try
        folder.CreateSubfolder(FolderName)
        UpdateTree()
    End Sub

    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
        Dim NewFeedDialog As New FeedDialog
        If NewFeedDialog.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Dim folder As IFeedFolder
        Try : folder = FeedsManager.GetFolder(TreeView1.SelectedNode.Name)
        Catch : folder = FeedsManager.RootFolder
        End Try
        folder.CreateFeed(NewFeedDialog.NameTextBox.Text, NewFeedDialog.URLTextBox.Text)
        UpdateTree()
    End Sub

    Private Sub EditButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditButton.Click
        Dim NewFeedDialog As New FeedDialog
        Dim Feed As IFeed = FeedsManager.GetFeed(TreeView1.SelectedNode.Name)
        NewFeedDialog.NameTextBox.Text = Feed.Name
        NewFeedDialog.URLTextBox.Text = Feed.Url
        If NewFeedDialog.ShowDialog() = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Feed.Rename(NewFeedDialog.NameTextBox.Text)
        Feed.Url = NewFeedDialog.URLTextBox.Text
        UpdateTree()
    End Sub

    Private Sub EditFolderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditFolderButton.Click
        Dim folder As IFeedFolder = FeedsManager.GetFolder(TreeView1.SelectedNode.Name)
        Dim Name As String = InputBox("Please enter new folder name")
        If Name = "" Then Exit Sub
        folder.Rename(Name)
    End Sub

    Private Sub StartAtStartup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartAtStartup.CheckedChanged
        If StartAtStartup.Checked = True Then
            My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue("RSS Feed Popper", Reflection.Assembly.GetExecutingAssembly().Location())
        Else
            Try
                My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue("RSS Feed Popper")
            Catch
            End Try
        End If
    End Sub
End Class
